package com.yc.jpyy.fileattachment.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.fileattachment.service.FileAttachmentService;
import com.yc.jpyy.utils.Tools;

@Controller
@RequestMapping(value = "/fileAttachment")
@Scope("prototype")
public class FileAttachmentController extends BaseController {

	@Autowired
	private FileAttachmentService fileAttachmentService;
	private static Logger logger = LoggerFactory.getLogger(FileAttachmentController.class);

	// 新增
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addFileAttachment", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String addFileAttachment(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));

		try {
			String info = request.getParameter("info");
			System.out.println("info" + info);
			// 设置数据源
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			System.out.println("paramsMap" + paramsMap);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			fileAttachmentService.addFileAttachmentService(paramsMap);
			logger.info("新增二保成功");
			response.setReturnMsg(1, "操作成功");
			return response.toJSON();

		} catch (Exception e) {
			logger.error("新增二保成功处理异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			response.setReturnException(e.toString());
			e.printStackTrace();
		}

		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}

	}

	// 删除处理 物理删除
	@SuppressWarnings("finally")
	@RequestMapping(value = "/delFileAttachment", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String delFileAttachment(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			String info = request.getParameter("info");
			System.out.println("info" + info);
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			System.out.println("paramsMap" + paramsMap);
			String ids = paramsMap.get("ids").toString();
			String[] idarray = ids.split(",");
			paramsMap.put("idarray", idarray);
			String dbcode = paramsMap.get("dbcode").toString();
			// 设置数据源
			HandlerDataSource.setDataSource(dbcode);
			int ret = fileAttachmentService.delFileAttachmentService(paramsMap);
			if (ret > 0) {
				response.setReturnMsg(1, "删除成功");
				return response.toJSON();
			} else {
				response.setReturnMsg(0, "删除失败");
				return response.toJSON();
			}

		} catch (Exception e) {
			logger.error("删除异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			response.setReturnException(e.toString());
			e.printStackTrace();
		}

		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}

	}

	// 查询单条
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findFileAttachmentByID", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findFileAttachmentByID(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			System.out.println("paramsMap" + paramsMap);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			
			// 设置数据源
			HandlerDataSource.setDataSource(dbcode);
			Map<String, Object> val = fileAttachmentService.findFileAttachmentByIDServer(paramsMap);
			int retSize = val.size();
			if (retSize == 0) {
				response.setReturnMsg(1, "无查询数据");
				return response.toJSON();
			} else {
				response.setReturnMsg(1, "查询单条数据成功");

				response.setListData(val);
				return response.toJSON();
			}

		} catch (Exception e) {
			logger.error("查询单条数据异常" + e.toString());
			// 异常信息
			response.setReturnException(e.toString());
			response.setReturnCode(0);
			response.setReturnMsg("查询单条数据异常");

		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	// 修改
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateFileAttachment", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateFileAttachment(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));

		try {
			String info = request.getParameter("info");
			System.out.println(info);
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			// 设置数据源
			HandlerDataSource.setDataSource(dbcode);
			fileAttachmentService.updateFileAttachmentService(paramsMap);
			response.setReturnMsg(1, "操作成功");
			return response.toJSON();

		} catch (Exception e) {
			logger.info("修改违章处理异常" + e.toString());
			// 异常信息
			response.setReturnCode(0);
			response.setReturnMsg("操作失败");
			e.printStackTrace();
		}

		finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}

	}

	// 查询所有（查询条件并且分页）
	@SuppressWarnings("finally")
	@RequestMapping(value = "/findFileAttachment", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findFileAttachment(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			System.out.println("info" + info);
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			// 设置数据源
			HandlerDataSource.setDataSource(dbcode);
			List<Map<String, Object>> list = fileAttachmentService.findFileAttachmentService(paramsMap);
			response.setReturnMsg(1, "操作成功");
			response.setListData(list);
			return response.toJSON();

		} catch (Exception e) {
			logger.error("findCompany" + e.toString());
			response.setReturnCode(1);
			response.setReturnMsg("查询失败");
			response.setReturnException(e.toString());
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

	@SuppressWarnings("finally")
	@RequestMapping(value = "/findFileManagers", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String findFileManagers(HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		request.setCharacterEncoding("UTF-8");
		try {
			String info = request.getParameter("info");
			Map<String, Object> paramsMap = Tools.JsonToMap(info);
			String fileDir = paramsMap.get("getsettingvalue").toString().trim();
			if(fileDir.equals("UpServerUrl")) {
				response.setReturnMsg(1, "操作成功");
				response.setListData(Config.getUpServerUrl());
				return response.toJSON();
			}else if(fileDir.equals("UpLastver")) {
				HandlerDataSource.setDataSource("A1");
				String UpLastver = fileAttachmentService.getUpLastver(paramsMap);
				response.setReturnMsg(1, "操作成功");
				response.setListData(UpLastver);
				return response.toJSON();
			}else if(fileDir.equals("UpEnabled")) {
				response.setReturnMsg(1, "操作成功");
				response.setListData(Config.getUpEnabled());
				return response.toJSON();
			}else if(fileDir.equals("LogonPeriod")) {
				response.setReturnMsg(1, "操作成功");
				response.setListData(Config.getLogonPeriod());
				return response.toJSON();
			}else {
				response.setReturnMsg(0, "无此配置");
			}

		} catch (Exception e) {
			
			response.setReturnCode(0);
			response.setReturnMsg("查询失败");
			response.setReturnException(e.toString());
		} finally {
			HandlerDataSource.clear();
			return response.toJSON();
		}
	}

}
